import { defineStore } from "pinia";

export  const storeA = defineStore("storeA", {
  state: () => {
    const Cartfood:cartfood[]=[]
    const user:user = {
      Username: '',
      Password: '',
      Type: '',
      Phone:123,
      Address:"",
    }
    const imageUrl = ""
    const times:number = 0
    const  isHost:boolean=false
    const token:string=""
    const foods:food[]=[
  {
    name: "宫保鸡丁",
    url: "../img/1.png",
    text: "六六六ASDAS的 阿松大啊打发撒更容易太热士大夫阿泰噶山豆根伐尔尕噶尔啊",
    price: 20,
    business: "成理大酒店"
  },
  {
    name: "鸡肉丸子",
    url: "../img/3.png",
    text: "采用上等冷鲜肉炸制而成",
    price: 10,
    business: "成理大酒店"
  },
  {
    name: "重庆小面",
    url: "../img/4.png",
    text: "经典重庆老味道",
    price: 20,
    business: "成理大酒"
  },
  {
    name: "重庆小面",
    url: "../img/4.png",
    text: "经典重庆老味道",
    price: 20,
    business: "成理大酒店"
  }
    ]
    return {
        isNoLogin:true,
        Cartfood,
        times,
        foods,
        user,
        imageUrl,
        isHost,
        token
    };
  },
  getters: {
    test():cartfood[]{
      console.log("值已改变")
      console.log(this.Cartfood)
      return this.Cartfood
    }
  },
  actions: {
    setisNoLogin(data:boolean){
        this.$state.isNoLogin=data
    },
    addCartfood(data:cartfood){
      var ishave = false;
      this.Cartfood.forEach((item)=>{
        if(item.name.name===data.name.name){
          item.num+=data.num
          console.log("相同")
          ishave = true
        }
      })
      if(!ishave){
        this.Cartfood.push(data)
        console.log("新添加")
      }
    },
    setCartfood(){
      for(let i = 0;i<this.Cartfood.length;i++){
        for(let n=i+1;n<this.Cartfood.length;n++){
          if(this.Cartfood[i].name===this.Cartfood[n].name){
            console.log("相等")
            this.Cartfood[i].num+=this.Cartfood[n].num
            this.$state.Cartfood.splice(n,1)
          }
        }
      }
    },
    delCartfood(data:string){
      var del = 100
      this.$state.Cartfood.forEach((item,index)=>{
        if(item.name.name===data){
          del = index
        }
      })
      this.$state.Cartfood.splice(del,1)
    },
    
  },
});
